#include<iostream>
#include<unistd.h>
using namespace std;

int main(){
    int a[10][10], supp[10], dem[10], o, d, res[10][10];
    cout << "Enter the No. of Origins: ";
    cin >> o;
    cout << "Enter the No. of Destinations: ";
    cin >> d;
    cout << "Enter the Cost Matrix: \n";
    for(int i = 0; i < o; i++){
        for(int j = 0; j < d; j++){
            cout << "Enter O[" << i+1 << "] D[" << j+1 << "] : ";
            cin >> a[i][j];
        }
    }
    int sum_s = 0, sum_d = 0;
    cout << "Enter the Supply: \n";
    for(int i = 0; i < o; i++){
        cout << "Supply " << i+1 << " : ";
        cin >> supp[i];
        sum_s += supp[i];
    }
    cout << "Enter the Demand: \n";
    for(int i = 0; i < d; i++){
        cout << "Demand " << i+1 << " : ";
        cin >> dem[i];
        sum_d += dem[i];
    }
    if(sum_d < sum_s){
        d++;
        cout << "\nTotal Demand Less than Supply..\nCompensating..\n";
        sleep(2);
        cout << "Compensated !!\n";
        for(int i = 0; i < o; i++){
            a[i][d-1] = 0;
        }
        dem[d-1] = sum_s - sum_d;
    }
    if(sum_s < sum_d){
        o++;
        cout << "\nTotal Supply Less than Demand..\nCompensating..\n";
        sleep(2);
        cout << "Compensated !!\n";
        for(int i = 0; i < d; i++){
            a[o-1][i] = 0;
        }
        supp[o-1] = sum_d - sum_s;
        sum_s = sum_d;
    }
    sleep(1);
    cout << "The Final Matrix is: \n";
    sleep(1);
    char mach = 65;
    int job = 1;
    for(int i = -1; i < o; i++){
        if(i < 0){
            cout << "\t";
            for(int k = 0; k < d; k++){
                cout << mach << "\t";
                mach++;
            }
            cout << "\tSupply";
            cout << "\n";
            continue;
        }
        cout << "Job " << job << "\t";
        job++;
        for(int j = 0; j < d; j++){
            cout << a[i][j] << "\t";
        }
        cout << "|\t" << supp[i];
        cout << endl;
    }
    for(int i = 0; i <= d+1; i++){
        cout << "\t_";
    }
    cout << endl;
    for(int i = 0; i <= d; i++){
        if(i == d){
            cout << "\t|\t" << sum_s << "\t";
            break;
        }
        if(i == 0){
            cout << "Demand";
        }
        cout << "\t" << dem[i];
    }
    for(int i = 0; i < o; i++){
        for(int j = 0; j < d; j++){
            res[i][j] = 0;
        }
    }
    for(int i = 0; i < o; i++){
        for(int j = 0; j < d; j++){
            if(supp[i] == 0){
                break;
            }
            if(dem[j] == 0){
                continue;
            }
            if(supp[i] < dem[j] && supp[i] != 0){
                dem[j] -= supp[i];
                res[i][j] = supp[i];
                supp[i] == 0;
            }
            else if(supp[i] > dem[j] && dem[j] != 0){
                supp[i] -= dem[j];
                res[i][j] = dem[j];
                dem[j] = 0;
            }
        }
    }
    cout << "\nThe Resultant Matrix: \n";
    sleep(2);
    for(int i = 0; i < o; i++){
        for(int j = 0; j < d; j++){
            cout << res[i][j] << "\t";
        }
        cout << endl;
    }
    return 0;
}